package com.qfedu.yuxiaoer.controller;

import com.qfedu.yuxiaoer.common.api.CommonResult;
import com.qfedu.yuxiaoer.dto.BillQueryParam;
import com.qfedu.yuxiaoer.entity.Bill;
import com.qfedu.yuxiaoer.entity.BillCostItem;
import com.qfedu.yuxiaoer.service.BillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Api(tags = "BillController",description = "账单")
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @ApiOperation(value = "根据id查找账单详情")
    @RequestMapping(value = "/find/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Bill> findBillDetails(@PathVariable Integer id){
        return CommonResult.success(billService.selectBillDetailsById(id));
    }

    @ApiOperation(value = "根据条件加载账单列表")
    @RequestMapping(value = "/findList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Bill>> findBillList(BillQueryParam queryParam){
        List<Bill> billList = billService.selectBillList(queryParam);
        return CommonResult.success(billList);
    }

    @ApiOperation(value = "在编辑账单中添加费用项")
    @RequestMapping(value = "/edit/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addInEdit(BillCostItem billCostItem,Integer billId){
        int count = billService.addCostItem(billCostItem,billId);
        if (count >= 2){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    @ApiOperation(value = "在编辑账单中删除费用项")
    @RequestMapping(value = "/edit/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult deleteInEdit(Integer billId,Integer billCostItemId){
        int count = billService.deleteCostItem(billId,billCostItemId);
        if (count >= 2){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }
}
